﻿/*
Copyright (c) 2016 Robert Gaffney
Licensed under the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Kemper_Duplicate_Finder
{
    class MatchEngine
    {
        public List<string> srcFiles;
        public List<string> destFiles;
        public Dictionary<string, SourceFile> sourceFiles;
        //Dictionary<string, MidiTag> allTags;
        Dictionary<string, List<string>> sourcefilesByTag;
        Dictionary<string, List<string>> destfilesByTag;
        Dictionary<string, List<string>> sourcefilesByAmp;
        Dictionary<string, List<string>> destfilesByAmp;
        Dictionary<string, List<string>> sourcefilesByCab;
        Dictionary<string, List<string>> destfilesByCab;
        public Dictionary<string, FileReader> sourceFilesDict;
        public Dictionary<string, FileReader> destFilesDict;
        public List<string> duplicateTags;

        public MatchEngine()
        {
            this.srcFiles = new List<string>();
            this.destFiles = new List<string>();

            this.sourceFiles = new Dictionary<string, SourceFile>();

            this.sourceFilesDict = new Dictionary<string, FileReader>();
            this.destFilesDict = new Dictionary<string, FileReader>();

            // populate tag-to-files dictionaries
            this.sourcefilesByTag = new Dictionary<string, List<string>>();
            this.destfilesByTag = new Dictionary<string, List<string>>();

            // populate amp/cab-to-files dictionaries
            this.sourcefilesByAmp = new Dictionary<string, List<string>>();
            this.destfilesByAmp = new Dictionary<string, List<string>>();
            this.sourcefilesByCab = new Dictionary<string, List<string>>();
            this.destfilesByCab = new Dictionary<string, List<string>>();

            this.duplicateTags = new List<string>();
        }

        public  void run(string sourceFolder, string destFolder)
        {
            this.getFiles(sourceFolder, this.srcFiles);
            this.getFiles(destFolder, this.destFiles);

            // populate filereader dictionaries
            this.sourceFilesDict = new Dictionary<string, FileReader>();
            this.destFilesDict = new Dictionary<string, FileReader>();
            this.fillFileReaderDict(this.srcFiles, this.sourceFilesDict);
            this.fillFileReaderDict(this.destFiles, this.destFilesDict);

            // populate tag-to-files dictionaries
            this.sourcefilesByTag = new Dictionary<string, List<string>>();
            this.destfilesByTag = new Dictionary<string, List<string>>();
            this.fillFilesByTagDict(this.sourceFilesDict, this.sourcefilesByTag);
            this.fillFilesByTagDict(this.destFilesDict, this.destfilesByTag);

            // populate amp/cab-to-files dictionaries
            this.sourcefilesByAmp = new Dictionary<string, List<string>>();
            this.destfilesByAmp = new Dictionary<string, List<string>>();
            this.sourcefilesByCab = new Dictionary<string, List<string>>();
            this.destfilesByCab = new Dictionary<string, List<string>>();
            this.fillFilesByAmpCabDict(this.sourceFilesDict, this.sourcefilesByAmp, this.sourcefilesByCab);
            this.fillFilesByAmpCabDict(this.destFilesDict, this.destfilesByAmp, this.destfilesByCab);

            this.getSourceFiles();
            this.countDuplicateTags();
        }

        private void getFiles(string folder, List<string> files)
        {
            string[] fileNames = Directory.GetFiles(folder);
            foreach (string fileName in fileNames)
            {
                if (fileName.ToLower().EndsWith(".kipr"))
                {
                    files.Add(fileName);
                }
            }
            string[] folderNames = Directory.GetDirectories(folder);
            foreach (string subFolder in folderNames)
            {
                this.getFiles(subFolder, files);
            }
        }

        private void fillFileReaderDict(List<string> files, Dictionary<string, FileReader> dict)
        {
            foreach (string filename in files)
            {
                FileReader fr = new FileReader(filename);
                fr.parse();
                FileReader dummy;
                if (!dict.TryGetValue(filename, out dummy))
                {
                    dict.Add(filename, fr);
                }
            }
        }

        private void fillFilesByTagDict(Dictionary<string, FileReader> fileDict, Dictionary<string, List<string>> dict)
        {
            // IMPORTANT! - Only looking at 0x0004 Type tags - (assumed profile data)
            foreach (KeyValuePair<string, FileReader> entry in fileDict)
            {
                foreach (KeyValuePair<string, MidiTag> entry2 in entry.Value.profileTags)
                {
                    List<string> fileList;
                    if (!dict.TryGetValue(entry2.Value.hash, out fileList))
                    {
                        fileList = new List<string>();
                        dict.Add(entry2.Value.hash, fileList);
                    }
                    fileList.Add(entry.Key);
                }
            }
        }

        private void fillFilesByAmpCabDict(Dictionary<string, FileReader> fileDict, Dictionary<string, List<string>> ampDict, Dictionary<string, List<string>> cabDict)
        {
            foreach (KeyValuePair<string, FileReader> entry in fileDict)
            {
                string ampHash = entry.Value.ampHash;
                string cabHash = entry.Value.cabHash;

                List<string> fileList;

                if (!ampDict.TryGetValue(ampHash, out fileList))
                {
                    fileList = new List<string>();
                    ampDict.Add(ampHash, fileList);
                }
                fileList.Add(entry.Key);

                if (!cabDict.TryGetValue(cabHash, out fileList))
                {
                    fileList = new List<string>();
                    cabDict.Add(cabHash, fileList);
                }
                fileList.Add(entry.Key);
            }
        }

        private void getSourceFiles()
        {
            foreach (string srcFilename in this.srcFiles)
            {
                FileReader fr;
                if (this.sourceFilesDict.TryGetValue(srcFilename, out fr))
                {
                    SourceFile sourceFile = new SourceFile(fr);
                    sourceFile.getMatches("Amp", this.destfilesByAmp, this.destFilesDict);
                    sourceFile.getMatches("Cab", this.destfilesByCab, this.destFilesDict);
                    SourceFile dummy;
                    if (!sourceFiles.TryGetValue(srcFilename, out dummy))
                    {
                        sourceFiles.Add(srcFilename, sourceFile);
                    }
                }
            }
        }

        private void countDuplicateTags()
        {
            foreach (KeyValuePair<string, SourceFile> entry in this.sourceFiles)
            {
                SourceFile sourceFile = entry.Value;

                foreach (KeyValuePair<string, FileReader> dupeFile in sourceFile.ampMatchFiles)
                {
                    if (!duplicateTags.Contains(dupeFile.Value.ampHash))
                    {
                        duplicateTags.Add(dupeFile.Value.ampHash);
                    }
                }
                foreach (KeyValuePair<string, FileReader> dupeFile in sourceFile.cabMatchFiles)
                {
                    if (!duplicateTags.Contains(dupeFile.Value.cabHash))
                    {
                        duplicateTags.Add(dupeFile.Value.cabHash);
                    }
                }                
            }
        }
    }
}
